import QtQuick 1.1
import com.nokia.meego 1.0
import REngine 1.0
Rectangle {
    id: viewer
    signal clicked
    signal pressAndHold
    radius: 5
    color: "bisque"
    //Text view
    property string fileName: ""
    property int startLine: 0
    property int startChar: 0
    property alias viewFontSize: textView.font.pixelSize
    property alias viewFontFamily: textView.font.family
    property alias viewFontColor: textView.color

    //Title Rectangle
    property alias titleText: titleText.text
    property alias titleFontSize: titleText.font.pixelSize
    property alias titleHeight: titleRect.height

    //Info Rectangle
    property alias infoHeight: infoRect.height
    property alias info1Text: info1.text
    property alias info1FontSize: info1.font.pixelSize
    property alias info2Text: info2.text
    property alias info2FontSize: info2.font.pixelSize

    function loadText() {
        var lineNum = viewer.startLine
        //First load line's left chars
        var line = engine.getLine(viewer.fileName, viewer.startLine)
        //For safe
        if (viewer.startChar > line.length - 1 || viewer.startChar < 0) {
            console.log("ERROR::Invalid charIndex:", viewer.startChar,
                        "Max charIndex:", line.length - 1)
            if (viewer.startChar > line.length - 1) {
                viewer.startChar = line.length - 1
            }

            if (viewer.startChar < 0) {
                viewer.startChar = 0
            }
        }
        textView.text = line.substring(viewer.startChar)
        while (textView.paintedHeight <= textView.height) {
            //If line is this last line, do not get next line
            if (lineNum === engine.fileLineNumber(viewer.fileName) -  1) {
                console.log("Reaches the last line, do not load next line")
                break
            }

            line = engine.getLine(viewer.fileName, ++lineNum)
            textView.text += line
            console.log("painted height", textView.paintedHeight,
                        "target height", textView.height)
        }
    }

    function showOpenFileIndicator(show) {
        if (show) {
            openFileIndicator.running = true
            openFileIndicator.opacity = 1
        }
        else {
            openFileIndicator.running = false
            openFileIndicator.opacity = 0
        }
    }

    function openFileFinished(fileName, result)  {
        console.debug("openFileFinished: fileName", fileName,
                      "result:", result)
        if (fileName === viewer.fileName) {
            showOpenFileIndicator(false)
            if (result === REngine.OpenFileSuccess ||
                    result === REngine.OpenFileSuccessWithDefaultCodec) {
                loadText()
            }
            else {
            }
        }
        else {
            console.log("fileName is not the same, current:", viewer.fileName)
        }
    }


    Text {
        id: textView
        clip: true
        smooth: true
        anchors.top: parent.top
        anchors.bottom: titleRect.top
        font.pixelSize: 20
        font.family: "Arial"
        width: parent.width
        wrapMode: Text.WordWrap
        lineHeightMode: Text.FixedHeight
        lineHeight:  height/Math.floor(height/font.pixelSize)
    }

    Item {
        id: titleRect
        anchors.bottom: infoRect.top
        width: parent.width
        height: 30
        Label {
            id: titleText
            anchors.centerIn: parent
            width: parent.width
            elide: Text.ElideRight
            horizontalAlignment: Text.AlignHCenter
            font.pixelSize: 20
            font.bold: true
            color: viewer.viewFontColor
        }
    }
    Item {
        id: infoRect
        anchors.bottom: parent.bottom
        width: parent.width
        height: 20
        Label {
            id: info1
            anchors.left: parent.left
            anchors.verticalCenter: parent.verticalCenter
            font.pixelSize: 18
            font.bold: true
            color: viewer.viewFontColor
        }

        Label {
            id: info2
            anchors.right: parent.right
            anchors.verticalCenter: parent.verticalCenter
            font.pixelSize: 18
            font.bold: true
            color: viewer.viewFontColor
        }
    }

    BusyIndicator {
        id: openFileIndicator
        anchors.centerIn: parent
        platformStyle: BusyIndicatorStyle { size: "small" }
    }

//    Connections {
//        target: viewer
//        onFileNameChanged: loadText()
//        onStartLineChanged: loadText()
//        onStartCharChanged: loadText()
//        onViewFontSizeChanged: loadText()
//    }

//    Connections {
//        target: textView
//        onHeightChanged: loadText()
//        onWidthChanged: loadText()
//        onLineHeightChanged: loadText()
//    }

    Component.onCompleted: {
        console.log("Thumbnail width:", viewer.width, "height:", viewer.height)
        console.log("Thumbnail open file:",viewer.fileName)
        engine.asyncOpenFile(viewer.fileName)
        showOpenFileIndicator(true)
    }

    Connections {
        target: engine
        onFileOpened: openFileFinished(fileName, result)
    }

    SequentialAnimation {
        id: clickAnimation
        running: false
        NumberAnimation {
            target: viewer;
            property: "scale";
            to: 0.86;
            duration: 80
            easing.type: Easing.InQuad
        }
        NumberAnimation {
            target: viewer;
            property: "scale";
            to: 1;
            duration: 50
            easing.type: Easing.OutBack
        }

        ScriptAction {script: viewer.clicked()}
    }


//    SequentialAnimation {
//        id: pressAndHoldAnimation
//        running: false
//        NumberAnimation {
//            target: viewer;
//            property: "scale";
//            to: 0.86;
//            duration: 80
//            easing.type: Easing.InQuad
//        }

//        ScriptAction {script: viewer.pressAndHold()}
//    }
    MouseArea {
        id: mouseArea
        anchors.fill: parent
        onClicked: {
            clickAnimation.running = true
        }
        onPressAndHold: {
            viewer.state = "PressAndHold"
//            pressAndHoldAnimation.running = true
            viewer.pressAndHold();
        }
    }

    states: [
        State {
            name: "Normal"
            PropertyChanges {
                target: viewer
                scale: 1.0
            }
        },
        State {
            name: "PressAndHold"
            PropertyChanges {
                target: viewer
                scale: 0.92
            }
        }
    ]

    transitions: [
        Transition {
            from: "Normal"
            to: "PressAndHold"
            reversible : true
            NumberAnimation{properties: "scale"; easing.type: Easing.InOutQuad}
        }
    ]
}
